package service

import (
	"encoding/json"
	"example/model"
	"fmt"
	"gitee.com/baizhige/message-go"
)

// 注册事件监听
func init() {
	message.Listen[model.UserCreateMsg](SendCoupon)
	message.Listen[model.UserCreateMsg](SendEmail)
}

// 模拟用户创建
func CreateUser() {
	//...一些逻辑 假装完成用户创建
	user := &model.User{
		Name: "User1",
		Age:  10,
	}
	fmt.Println("CreateUser complete:", *user)

	//发布用户创建完成消息
	msg := model.UserCreateMsg{
		User:      user,
		OtherInfo: "xxoo",
	}
	message.Publisher(msg) //发布消息
}

// 模拟监听用户创建发放卡券
func SendCoupon(msg model.UserCreateMsg) {
	marshal, _ := json.Marshal(msg)
	fmt.Println("SendCoupon msg receive:", string(marshal))
	fmt.Println("给用户发卡券了")
}

// 模拟监听用户创建发放卡券
func SendEmail(msg model.UserCreateMsg) {
	marshal, _ := json.Marshal(msg)
	fmt.Println("SendEmail msg receive:", string(marshal))
	fmt.Println("给用户发邮件了")
}
